package com.castle.fortress.admin.oa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.oa.entity.DayStatisticsEntity;
import com.castle.fortress.admin.oa.dto.DayStatisticsDto;
import com.castle.fortress.admin.oa.mapper.DayStatisticsMapper;
import com.castle.fortress.admin.oa.service.DayStatisticsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import com.castle.fortress.admin.utils.BizCommonUtil;

/**
 * 日数据报表 服务实现类
 *
 * @author whc
 * @since 2022-11-04
 */
@Service
public class DayStatisticsServiceImpl extends ServiceImpl<DayStatisticsMapper, DayStatisticsEntity> implements DayStatisticsService {
    /**
    * 获取查询条件
    * @param dayStatisticsDto
    * @return
    */
    public QueryWrapper<DayStatisticsEntity> getWrapper(DayStatisticsDto dayStatisticsDto){
        QueryWrapper<DayStatisticsEntity> wrapper= new QueryWrapper();
        if(dayStatisticsDto != null){
            DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto,DayStatisticsEntity.class);
            wrapper.eq(StrUtil.isNotEmpty(dayStatisticsEntity.getDate()),"date",dayStatisticsEntity.getDate());
            wrapper.eq(dayStatisticsEntity.getUserId() != null,"user_id",dayStatisticsEntity.getUserId());
        }
        return wrapper;
    }


    @Override
    public IPage<DayStatisticsDto> pageDayStatistics(Page<DayStatisticsDto> page, DayStatisticsDto dayStatisticsDto) {
        QueryWrapper<DayStatisticsEntity> wrapper = getWrapper(dayStatisticsDto);
        Page<DayStatisticsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<DayStatisticsEntity> dayStatisticsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<DayStatisticsDto> pageDto = new Page(dayStatisticsPage.getCurrent(), dayStatisticsPage.getSize(),dayStatisticsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(dayStatisticsPage.getRecords(),DayStatisticsDto.class));
        return pageDto;
    }

	@Override
	public IPage<DayStatisticsDto> pageDayStatisticsExtends(Page<DayStatisticsDto> page, DayStatisticsDto dayStatisticsDto){
		Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
          DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto,DayStatisticsEntity.class);
		List<DayStatisticsEntity> dayStatisticsList=baseMapper.extendsList(pageMap,dayStatisticsEntity);
		Long total = baseMapper.extendsCount(dayStatisticsEntity);
		Page<DayStatisticsDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
		pageDto.setRecords(ConvertUtil.transformObjList(dayStatisticsList,DayStatisticsDto.class));
		return pageDto;
	}
	@Override
	public DayStatisticsDto getByIdExtends(Long id){
          DayStatisticsEntity  dayStatisticsEntity = baseMapper.getByIdExtends(id);
		return ConvertUtil.transformObj(dayStatisticsEntity,DayStatisticsDto.class);
	}

    @Override
    public List<DayStatisticsDto> listDayStatistics(DayStatisticsDto dayStatisticsDto){
        QueryWrapper<DayStatisticsEntity> wrapper = getWrapper(dayStatisticsDto);
        List<DayStatisticsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,DayStatisticsDto.class);
    }

    @Override
    public ArrayList<Long> getExistsIds(String date) {


        return baseMapper.getExistsIds(date);

    }
}

